package com.alibaba.springboot.health.controller;

import com.alibaba.springboot.health.pojo.Page;
import com.alibaba.springboot.health.pojo.RegisterOrder;
import com.alibaba.springboot.health.service.RegisterOrderService;
import com.alibaba.springboot.health.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * (RegisterOrder)表控制层
 *
 * @author makejava
 * @since 2019-12-28 20:08:34
 */
@Controller
public class RegisterOrderController {
    @Autowired
    private RegisterOrderService orderService;

    @RequestMapping(value = "/upd",method = RequestMethod.POST)
    @ResponseBody
    public RegisterOrder upd(String orderId){
        RegisterOrder order = orderService.queryById(Integer.parseInt(orderId));
        int orderStatus=1;
        if (order.getOrderStatus()==1){
            orderStatus=0;
        }
        order.setOrderStatus(orderStatus);
        RegisterOrder update = orderService.update(order);
        return update;
    }

    @RequestMapping("/order")
    public String toOrder(Model model, String username, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (username != null && username != "") {
            List<RegisterOrder> userList = orderService.findAll(username);
            System.out.println(userList);
            Page<RegisterOrder> page1 = new Page<>(1, 5, userList.size());
            int startIndex = page1.getStartIndex();
            page1.setList(orderService.findLimitOrder(startIndex, 5, username));
            model.addAttribute("page", page1);
            session.setAttribute("keyword", username);
            return "order-list-qt";
        } else {
            List<RegisterOrder> userList = orderService.findAll("");
            Page<RegisterOrder> page1 = new Page<>(1, 5, userList.size());
            int startIndex = page1.getStartIndex();
            page1.setList(orderService.findLimitOrder(startIndex, 5, ""));
            model.addAttribute("page", page1);
            session.setAttribute("keyword", username);
            return "order-list-qt";
        }
    }
    @RequestMapping("/order/limit")
    public String findLimit(int ye, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("keyword");
        if (name == null) {
            List<RegisterOrder> userList = orderService.findAll("");
            Page<RegisterOrder> page1 = new Page<>(ye, 5, userList.size());
            int startIndex = page1.getStartIndex();
            page1.setList(orderService.findLimitUser(startIndex, 5, ""));
            model.addAttribute("page", page1);
            return "order-list-qt";
        } else {
            List<RegisterOrder> userList = orderService.findAll(name);
            Page<RegisterOrder> page1 = new Page<>(ye, 5, userList.size());
            int startIndex = page1.getStartIndex();
            page1.setList(orderService.findLimitUser(startIndex, 5, name));
            model.addAttribute("page", page1);
            return "order-list-qt";
        }
    }

    @RequestMapping("/del")
    @ResponseBody
    public boolean todel(String orderId){
        boolean b = orderService.deleteById(Integer.parseInt(orderId));
        return b;
    }

    @RequestMapping("/addCar")
    public String addCar(String doctorName, String tel, String name, String sex, int age, Model model, double price){
        Random random = new Random();
        String orderNum="";
        for (int i = 0; i < 8; i++) {
            orderNum+=random.nextInt(10);
        }
        RegisterOrder registerOrder = new RegisterOrder();
        registerOrder.setOrderDname(doctorName);
        registerOrder.setOrderNum(orderNum);
        registerOrder.setOrderUname(name);
        registerOrder.setOrderUage(age);
        registerOrder.setOrderUsex(sex);
        registerOrder.setOrderUtel(tel);
        registerOrder.setOrderTime(new Date());
        int num = orderService.add(registerOrder);
        if (num==1){
            return "start";
        }
        return "qrddy";
    }
    @RequestMapping("dlogin")
    public String todlogin(){
        return "dlogin";
    }
    @RequestMapping(value = "/orderLogin", method = RequestMethod.POST)
    @ResponseBody
    public int tologin(UserVo userVo, HttpServletRequest request) {
        System.out.println(userVo+"===============");
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(userVo.getName(), userVo.getPassword());
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            if (subject.isAuthenticated()) {
                HttpSession session = request.getSession();
                session.setAttribute("user", userVo);
                return 1;
            }
        } catch (AuthenticationException e) {
            System.out.println("认证失败");
        }
        return 0;
    }

}